package com.esyoga.yoyoga.handler;
import com.esyoga.yoyoga.service.CoachOrderService;
import com.esyoga.yoyoga.util.RegexUtil;
import com.esyoga.yoyoga.util.SecurityCodeUtil;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Data
@Controller
@RequestMapping("/getCode")
public class GetCodeHandler {
    /**
     * 获取注册验证码
     * @param phoneNumber
     * @param request
     * @return
     */
    @RequestMapping("/getRgisterCode")
    @ResponseBody
    public String getRegisterCode(String phoneNumber, HttpServletRequest request){
        System.out.println("传入的手机号："+phoneNumber);
        String state = "获取失败";
        //正则表达式判断手机号
        boolean flag = RegexUtil.isPhone(phoneNumber);
        if (flag){
            //获取验证码随机数
            //String code = SecurityCodeUtil.getRandomCode();
            HttpSession session = request.getSession();
            //生成四位数验证码存入session来验证
            session.setAttribute("registerCode", "1111");
            //存入手机号
            session.setAttribute("phoneNumber",phoneNumber);
          // CoachOrderService.SecurityCodeUtil.userRegisterCode(phoneNumber, code);
            state = "获取验证码成功";
        }
        System.out.println(state);
        return state;
    }

    /**
     * 获取登录验证码
     * @param loginPhoneNumber：登录手机号
     * @param request
     * @return
     */
    @RequestMapping("/getLoginCode")
    @ResponseBody
    public String getLoginCode(String loginPhoneNumber, HttpServletRequest request){

        String state = "获取失败";
        //正则表达式判断手机号
        boolean flag = RegexUtil.isPhone(loginPhoneNumber);
        if (flag){
            //获取验证码随机数
            //String loginCode = SecurityCodeUtil.getRandomCode();
            HttpSession session = request.getSession();
            //生成四位数验证码存入session来验证
            session.setAttribute("loginCode", "1111");
            //存入手机号
            session.setAttribute("loginPhoneNumber",loginPhoneNumber);
           // CoachOrderService.SecurityCodeUtil.userRegisterCode(loginPhoneNumber, loginCode);
            state = "获取成功";
        }
        System.out.println(state);
        return state;
    }
    /**
     * 获取找回密码验证码
     * @param restPhoneNumber：手机号
     * @param request
     * @return
     */
    @RequestMapping("/getRestCode")
    @ResponseBody
    public String getRestCode(String restPhoneNumber, HttpServletRequest request){

        String state = "获取失败";
        //正则表达式判断手机号
        boolean flag = RegexUtil.isPhone(restPhoneNumber);
        if (flag){
            //获取验证码随机数
            //String restCode = SecurityCodeUtil.getRandomCode();
            HttpSession session = request.getSession();
            //生成四位数验证码存入session来验证
            session.setAttribute("restCode", "1111");
            //存入手机号
            session.setAttribute("restPhoneNumber",restPhoneNumber);
            // CoachOrderService.SecurityCodeUtil.resetPasswordCode(restPhoneNumber, restCode);
            state = "获取成功";
        }
        System.out.println(state);
        return state;
    }
}
